package com.danielagarza.app.servicios;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.danielagarza.app.modelos.Persona;
import com.google.gson.Gson;

import android.location.Location;

public class PublicarDatos {
	
	private static String VINCULAR_URL = "http://li530-140.members.linode.com:9003/servicios/addContacto";
	private static String REGISTAR	 = "http://li530-140.members.linode.com:9003/servicios/addPersona";
	

	public void vincularUsuarios(Integer token, Boolean isParent, Location loca) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(PublicarDatos.VINCULAR_URL);

		try {

			List<NameValuePair> postValues = new ArrayList<NameValuePair>(4);
			postValues.add(new BasicNameValuePair("idPersona", "1"));
			postValues.add(new BasicNameValuePair("latitud", ""
					+ loca.getLatitude()));
			postValues.add(new BasicNameValuePair("longitud", ""
					+ loca.getLongitude()));
			postValues.add(new BasicNameValuePair("token", token.toString()));
			postValues.add(new BasicNameValuePair("esPadre", isParent
					.toString()));

			httppost.setEntity(new UrlEncodedFormEntity(postValues));
			httpclient.execute(httppost);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}

	}
	
	public Persona registrarUsuarios(Persona user) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(
				PublicarDatos.REGISTAR);

		try {
			List<NameValuePair> postValues = new ArrayList<NameValuePair>(4);
			postValues.add(new BasicNameValuePair("idFacebook", user.getIdFacebook()));
			postValues.add(new BasicNameValuePair("nombre", user.getNombre()));
			postValues.add(new BasicNameValuePair("apellido", user.getApellido()));
			postValues.add(new BasicNameValuePair("genero",user.getGenero()));
			postValues.add(new BasicNameValuePair("rangoEdad", user.getRangoEdad()));
			postValues.add(new BasicNameValuePair("aPolitica", user.getaPolitica()));
			postValues.add(new BasicNameValuePair("correo", user.getCorreo()));
			postValues.add(new BasicNameValuePair("fotoURL", user.getFotoURL()));
			System.out.println("llegue 1 paso ");
			httppost.setEntity(new UrlEncodedFormEntity(postValues));
			
			 ResponseHandler<String> responseHandler=new BasicResponseHandler();
			// HttpResponse response = httpclient.execute(httppost);
			 System.out.println("paso 1");
		     String responseBody = httpclient.execute(httppost, responseHandler);
		     Gson gson = new Gson();
		     System.out.println("paso 2");
		     user = gson.fromJson(responseBody, Persona.class);
		     System.out.println(responseBody);
		     //responseBody es un  JSON
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}
		
		return user;
	}
	

}
